package com.cangshi.listener;

import com.alibaba.fastjson.JSON;
import com.cangshi.entity.TaskType;
import com.cangshi.service.TaskService;
import com.cangshi.service.UserService;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * Created by 15c on 2017/4/16.
 */

public class MyServletContextListener implements ServletContextListener {


    @Override
    public void contextInitialized(ServletContextEvent sce) {

        final TaskService service = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(TaskService.class);


        new Thread() {
            public void run() {
                while (true) {
                    try {
                    int i=  service.setTaskOverdue();
                        System.out.print("已经过期"+i+"条");
//                        List<TaskType> taskType = service.selectAllType();
//                        System.out.println("-------------->>>>>>>>"+ JSON.toJSONString(taskType));

                        //休眠两分钟
                        Thread.sleep(120 * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
